package v1

import (
	"github.com/gin-gonic/gin"
	"mall_2.0/consts"
	"mall_2.0/pkg/utils/ctl"
	"mall_2.0/pkg/utils/log"
	"mall_2.0/service"
	"mall_2.0/types"
	"net/http"
)

// CreateProductHandler 创建商品
func CreateProductHandler() gin.HandlerFunc {
	return func(c *gin.Context) {
		var req types.ProductCreateReq
		if err := c.ShouldBind(&req); err != nil {
			log.LogrusObj.Infoln(err)
			c.JSON(http.StatusBadRequest, ErrorResponse(c, err))
			return
		}
		form, _ := c.MultipartForm()
		files := form.File["image"]
		l := service.GetProductSrv()
		resp, err := l.ProductCreate(c.Request.Context(), files, &req)
		if err != nil {
			log.LogrusObj.Infoln(err)
			c.JSON(http.StatusInternalServerError, ErrorResponse(c, err))
			return
		}
		c.JSON(http.StatusOK, ctl.RespSuccess(c, resp))
	}
}

// UpdateProductHandler 更新商品
func UpdateProductHandler() gin.HandlerFunc {
	return func(c *gin.Context) {
		var req types.ProductUpdateReq
		if err := c.ShouldBind(&req); err != nil {
			log.LogrusObj.Infoln(err)
			c.JSON(http.StatusBadRequest, ErrorResponse(c, err))
			return
		}
		l := service.GetProductSrv()
		resp, err := l.ProductUpdate(c.Request.Context(), &req)
		if err != nil {
			log.LogrusObj.Infoln(err)
			c.JSON(http.StatusInternalServerError, ErrorResponse(c, err))
			return
		}
		c.JSON(http.StatusOK, ctl.RespSuccess(c, resp))
	}
}

// DeleteProductHandler 删除商品
func DeleteProductHandler() gin.HandlerFunc {
	return func(c *gin.Context) {
		var req types.ProductDeleteReq
		if err := c.ShouldBind(&req); err != nil {
			log.LogrusObj.Infoln(err)
			c.JSON(http.StatusBadRequest, ErrorResponse(c, err))
			return
		}
		l := service.GetProductSrv()
		resp, err := l.ProductDelete(c.Request.Context(), &req)
		if err != nil {
			log.LogrusObj.Infoln(err)
			c.JSON(http.StatusInternalServerError, ErrorResponse(c, err))
			return
		}
		c.JSON(http.StatusOK, ctl.RespSuccess(c, resp))
	}
}

// ListProductsHandler 获取商品列表
func ListProductsHandler() gin.HandlerFunc {
	return func(c *gin.Context) {
		var req types.ProductListReq
		if err := c.ShouldBind(&req); err != nil {
			log.LogrusObj.Infoln(err)
			c.JSON(http.StatusBadRequest, ErrorResponse(c, err))
			return
		}
		if req.PageSize == 0 {
			req.PageSize = consts.BaseProductPageSize
		}
		l := service.GetProductSrv()
		resp, err := l.ProductList(c.Request.Context(), &req)
		if err != nil {
			log.LogrusObj.Infoln(err)
			c.JSON(http.StatusInternalServerError, ErrorResponse(c, err))
			return
		}
		c.JSON(http.StatusOK, ctl.RespSuccess(c, resp))
	}
}

// ShowProductHandler 获取商品详情
func ShowProductHandler() gin.HandlerFunc {
	return func(c *gin.Context) {
		var req types.ProductShowReq
		if err := c.ShouldBind(&req); err != nil {
			log.LogrusObj.Infoln(err)
			c.JSON(http.StatusBadRequest, ErrorResponse(c, err))
			return
		}
		l := service.GetProductSrv()
		resp, err := l.ProductShow(c.Request.Context(), &req)
		if err != nil {
			log.LogrusObj.Infoln(err)
			c.JSON(http.StatusInternalServerError, ErrorResponse(c, err))
			return
		}
		c.JSON(http.StatusOK, ctl.RespSuccess(c, resp))
	}
}

// SearchProductsHandler 搜索商品
func SearchProductsHandler() gin.HandlerFunc {
	return func(c *gin.Context) {
		var req types.ProductSearchReq
		if err := c.ShouldBind(&req); err != nil {
			log.LogrusObj.Infoln(err)
			c.JSON(http.StatusBadRequest, ErrorResponse(c, err))
			return
		}
		if req.PageSize == 0 {
			req.PageSize = consts.BaseProductPageSize
		}
		l := service.GetProductSrv()
		resp, err := l.ProductSearch(c.Request.Context(), &req)
		if err != nil {
			log.LogrusObj.Infoln(err)
			c.JSON(http.StatusInternalServerError, ErrorResponse(c, err))
			return
		}
		c.JSON(http.StatusOK, ctl.RespSuccess(c, resp))
	}
}

// ListProductImgHandler 商品图片列表
func ListProductImgHandler() gin.HandlerFunc {
	return func(c *gin.Context) {
		var req types.ListProductImgReq
		if err := c.ShouldBind(&req); err != nil {
			log.LogrusObj.Infoln(err)
			c.JSON(http.StatusBadRequest, ErrorResponse(c, err))
			return
		}
		l := service.GetProductSrv()
		resp, err := l.ProductImgList(c.Request.Context(), &req)
		if err != nil {
			log.LogrusObj.Infoln(err)
			c.JSON(http.StatusInternalServerError, ErrorResponse(c, err))
			return
		}
		c.JSON(http.StatusOK, ctl.RespSuccess(c, resp))
	}
}
